﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Core.BaseCore.Extensions;
using Chinaer.Core.Helper;
using Chinaer.Model.Common;

namespace Chinaer.Bll
{
    public class Area : Repository, IBll.IArea
    {
        private IDao.IAreaDAO areaDAO = new Dao.AreaDAO();
        public Model.Common.JsonResult Add(DTO.AreaDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }

            if (areaDAO.GetByName(t.AreaName) != null)
            {
                return new Model.Common.JsonResult()
                {
                    ErrorMessage = "地区名称已经存在",
                    Success = false,
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists
                };
            }
            if (areaDAO.GetByCode(t.AreaCode) != null)
            {
                return new Model.Common.JsonResult()
                {
                    ErrorMessage = "地区编码已经存在",
                    Success = false,
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists
                };
            }
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            Model.Entities.Model_Area model_Area = new Model.Entities.Model_Area();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Area);
            jsonResult.Success = areaDAO.Add(model_Area);
            return jsonResult;
        }



        public Model.Common.JsonResult Delete(DTO.AreaDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            Model.Entities.Model_Area model_Area = new Model.Entities.Model_Area();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Area);
            jsonResult.Success = areaDAO.Delete(model_Area);
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = areaDAO.Delete(id);
            return jsonResult;
        }

        public Model.Common.JsonResult Update(DTO.AreaDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_Area model_Address = new Model.Entities.Model_Area();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Address);
            jsonResult.Success = areaDAO.Update(model_Address);
            return jsonResult;
        }

        public DTO.AreaDTO GetByID(string id)
        {
            Model.Entities.Model_Area model_Address = areaDAO.GetByID(id);
            DTO.AreaDTO addressDTO = new DTO.AreaDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Address, addressDTO);
            return addressDTO;
        }

        public List<DTO.AreaDTO> GetAreaByCityID(string cityID)
        {
            List<Model.Entities.Model_Area> listModelArea = new List<Model.Entities.Model_Area>();
            listModelArea = areaDAO.GetByCityID(cityID);
            if (listModelArea == null)
            {
                return null;
            }
            List<DTO.AreaDTO> listArea = new List<DTO.AreaDTO>();
            IBll.ICity city = new Bll.City();
            foreach (var item in listModelArea)
            {
                DTO.AreaDTO areaDTO = new DTO.AreaDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, areaDTO);
                if (!string.IsNullOrEmpty(areaDTO.CityID))
                {
                    var cityDTO = city.GetByID(areaDTO.CityID);
                    if (cityDTO != null)
                    {
                        areaDTO.CityName = cityDTO.CityName;
                    }
                }
                listArea.Add(areaDTO);
            }
            return listArea;
        }
    }
}
